/**
 * 登录功能JS文件
 * 
 * @author		布衣才子
 * @date		2012-10-03
 * @email		work.jerryliu@gmail.com
 * @qq			394969553
 * @version		v1.0
 * @copyright	copyright 2012-2014	YeeYi.com All Rights Reserved	
 */

/**
 * API Host
 */
var apiHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";
var b64 = new Base64();

/**
 * 登录亿忆网
 */
function loginYY(){
	var username = $('#username').val();
	var password = $('#password').val();
	if(username.length < 1){
		uexWindow.toast("0","5","登录失败，用户名不能为空…","2000");
		return false;
	}

	var uname = b64.encode(username);
	var pwd = b64.encode(password);
	uexWindow.toast("1","5","登录中，请稍后…","0");
	var loginURL = apiHost + '&ac=login&username='+uname+'&pwd='+pwd;
	xmlHttp(loginURL,showLoginResult);	
}

/**
 * 网络访问函数
 * @param {Object} url	请求的网络地址
 * @param {Object} callback	回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 登录回调函数
 * @param {Object} data
 */
function showLoginResult(data){
	uexWindow.closeToast();
	var returnCode = data.code;
	if(returnCode == 404){
		uexWindow.toast("0","5","登录失败，用户名或密码错误…","2000");
	}else if(returnCode == 200){
		uexWindow.toast("0","5","登录成功！","2000");
		localStorage.setItem('uid',data.uid);
		localStorage.setItem('username',b64.decode(data.username));
		localStorage.setItem('groupid',data.groupid);
		localStorage.setItem('pwd',data.pwd);
		var returnType = getLocalStorge('returnType');
		if(returnType == 'forum'){
			uexWindow.evaluateScript("root","0","hideLogin()");
			uexWindow.evaluatePopoverScript('root','content','init()');
		}else if(returnType == 'forum_list'){
			uexWindow.evaluateScript("root","0","hideLogin()");
			uexWindow.evaluatePopoverScript('root','content','init()');
			uexWindow.evaluatePopoverScript('forum_list','content','init()');
		}else if(returnType == 'viewthread'){
			uexWindow.evaluateScript("root","0","hideLogin()");
			uexWindow.evaluatePopoverScript('root','content','init()');
			uexWindow.evaluatePopoverScript('forum_list','content','init()');
			uexWindow.evaluatePopoverScript('threadWindow','content','init()');
		}
		
		//延迟两秒跳转窗口
		setTimeout(function () {
			uexWindow.close(-1);
		}, 2000);
	}else{
		uexWindow.toast("0","5","登录失败，未知错误…","2000");
	}
}

/**
 * HTML5 localStorge.getItem 函数的封装
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}